define([
  "jquery",
  "bootstrap",
  "backend",
  "addtabs",
  "table",
  "echarts",
  "echarts-theme",
  "template",
], function (
  $,
  undefined,
  Backend,
  Datatable,
  Table,
  Echarts,
  undefined,
  Template
) {
  /**
   * 订单状态 1待付款 2待接单 3已接单 4派送中 5已完成 6已取消
   */
  class OrderStatus {
    static PENDING_PAYMENT = 1;
    static TO_BE_CONFIRMED = 2;
    static CONFIRMED = 3;
    static DELIVERY_IN_PROGRESS = 4;
    static COMPLETED = 5;
    static CANCELLED = 6;
  }

  var Controller = {
    index: function () {
      // 初始化表格参数配置
      Table.api.init({
        extend: {
          index_url: "orders/index" + location.search,
          add_url: "orders/add",
          edit_url: "orders/edit",
          del_url: "orders/del",
          multi_url: "orders/multi",
          import_url: "orders/import",
          table: "orders",
        },
      });

      var table = $("#table");

      // 初始化表格
      table.bootstrapTable({
        url: $.fn.bootstrapTable.defaults.extend.index_url,
        pk: "id",
        sortName: "id",
        fixedColumns: true,
        fixedRightNumber: 1,
        commonSearch: false,
        search: false,
        searchFormVisible: false,
        showToggle: false,
        showColumns: false,
        showExport: false,
        queryParams: function (params) {
          params.type = OrderStatus.TO_BE_CONFIRMED;
          return params;
        },
        columns: [
          [
            { field: "number", title: __("Number"), operate: "LIKE" },
            {
              field: "phone",
              title: __("Phone"),
              operate: "LIKE",
              visible: false,
            },
            {
              field: "order_time",
              title: __("Order_time"),
              operate: "RANGE",
              addclass: "datetimerange",
              autocomplete: false,
            },
            {
              field: "order_dish",
              title: __("Order_dish"),
              table: table,
              class: "autocontent",
              formatter: Table.api.formatter.content,
              operate: false,
            },
            {
              field: "address",
              title: __("Address"),
              table: table,
              class: "autocontent",
              formatter: Table.api.formatter.content,
              operate: false,
            },
            {
              field: "estimated_delivery_time",
              title: __("estimatedDeliveryTime"),
              operate: "RANGE",
              addclass: "datetimerange",
              autocomplete: false,
              operate: false,
            },
            {
              field: "remark",
              title: __("Remark"),
              table: table,
              class: "autocontent",
              formatter: Table.api.formatter.content,
              operate: false,
            },
            {
              field: "tableware_number",
              title: __("tablewareNumber"),
              operate: false,
            },
            {
              field: "status",
              title: __("Status"),
              operate: false,
              visible: false,
              searchList: {
                1: __("Status 1"),
                2: __("Status 2"),
                3: __("Status 3"),
                4: __("Status 4"),
                5: __("Status 5"),
                6: __("Status 6"),
                7: __("Status 7"),
              },
              formatter: Table.api.formatter.status,
            },

            {
              field: "buttons",
              width: "120px",
              title: __("操作"),
              table: table,
              events: Table.api.events.operate,
              operate: false,
              buttons: [
                {
                  name: "jiedna",
                  text: __("接单"),
                  title: __("接单"),
                  classname: "btn btn-xs btn-success btn-magic btn-ajax",
                  icon: "fa fa-magic",
                  url: "ajax/acceptOrder",
                  confirm: "确定接单",
                  success: function (data, ret) {
                    // Layer.alert(ret.msg + ",返回数据：" + JSON.stringify(data));
                    //如果需要阻止成功提示，则必须使用return false;
                    table.bootstrapTable("refresh"); // 刷新表格
                    return false;
                  },
                  error: function (data, ret) {
                    console.log(data, ret);
                    Layer.alert(ret.msg);
                    return false;
                  },
                  visible: function (row) {
                    //返回true时按钮显示,返回false隐藏
                    return [2].includes(row.status);
                  },
                },
                {
                  name: "paisong",
                  text: __("派送"),
                  title: __("派送"),
                  classname: "btn btn-xs btn-success btn-magic btn-ajax",
                  icon: "fa fa-magic",
                  url: "ajax/deliveryOrder",
                  confirm: "确定派送",
                  success: function (data, ret) {
                    // Layer.alert(ret.msg + ",返回数据：" + JSON.stringify(data));
                    //如果需要阻止成功提示，则必须使用return false;
                    table.bootstrapTable("refresh"); // 刷新表格
                    return false;
                  },
                  error: function (data, ret) {
                    console.log(data, ret);
                    Layer.alert(ret.msg);
                    return false;
                  },
                  visible: function (row) {
                    //返回true时按钮显示,返回false隐藏
                    return [3].includes(row.status);
                  },
                },
                {
                  name: "wancheng",
                  text: __("完成"),
                  title: __("完成"),
                  classname: "btn btn-xs btn-success btn-magic btn-ajax",
                  icon: "fa fa-magic",
                  url: "ajax/compOrder",
                  confirm: "确认完成",
                  success: function (data, ret) {
                    // Layer.alert(ret.msg + ",返回数据：" + JSON.stringify(data));
                    //如果需要阻止成功提示，则必须使用return false;
                    table.bootstrapTable("refresh"); // 刷新表格
                    return false;
                  },
                  error: function (data, ret) {
                    console.log(data, ret);
                    Layer.alert(ret.msg);
                    return false;
                  },
                  visible: function (row) {
                    //返回true时按钮显示,返回false隐藏
                    return [4].includes(row.status);
                  },
                },
                {
                  name: "tuidan",
                  text: __("退单"),
                  title: __("退单"),
                  classname: "btn btn-xs btn-success btn-magic btn-ajax",
                  icon: "fa fa-magic",
                  url: "ajax/returnOrder",
                  success: function (data, ret) {
                    // Layer.alert(ret.msg + ",返回数据：" + JSON.stringify(data));
                    //如果需要阻止成功提示，则必须使用return false;
                    table.bootstrapTable("refresh"); // 刷新表格
                    return false;
                  },
                  error: function (data, ret) {
                    console.log(data, ret);
                    Layer.alert(ret.msg);
                    return false;
                  },
                  visible: function (row) {
                    //返回true时按钮显示,返回false隐藏
                    return [5].includes(row.status);
                  },
                },
                {
                  name: "cancel",
                  text: __("取消"),
                  title: __("取消原因"),
                  classname: "btn btn-xs btn-success btn-magic btn-ajax",
                  classname: "btn btn-xs btn-primary btn-dialog",
                  icon: "fa fa-list",
                  url: "orders/cancel",
                  callback: function (data) {
                    // Layer.alert("接收到回传数据：" + JSON.stringify(data), {
                    //   title: "回传数据",
                    // });
                  },
                  visible: function (row) {
                    //返回true时按钮显示,返回false隐藏
                    return [1, 3, 4].includes(row.status);
                  },
                },
                {
                  name: "detail",
                  text: __("查看"),
                  title: __("订单信息"),
                  classname: "btn btn-xs btn-success btn-magic btn-ajax",
                  classname: "btn btn-xs btn-primary btn-dialog",
                  icon: "fa fa-list",
                  url: "orders/detail",
                  callback: function (data) {
                    Layer.alert("接收到回传数据：" + JSON.stringify(data), {
                      title: "回传数据",
                    });
                  },
                  visible: function (row) {
                    //返回true时按钮显示,返回false隐藏
                    return true;
                  },
                },
              ],
              formatter: Table.api.formatter.buttons,
            },
            // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
          ],
        ],
      });

      // 为表格绑定事件
      Table.api.bindevent(table);

      //绑定TAB事件
      $('a[data-toggle="tab"]').on("shown.bs.tab", function (e) {
        // var options = table.bootstrapTable(tableOptions);
        var typeStr = $(this).attr("href").replace("#", "");
        var options = table.bootstrapTable("getOptions");
        options.pageNumber = 1;
        options.queryParams = function (params) {
          // params.filter = JSON.stringify({type: typeStr});
          params.type = typeStr;

          return params;
        };
        table.bootstrapTable("refresh", {});
        return false;
      });


      $("#addDish").click(function (e) {
        e.preventDefault();
        Fast.api.open("dish/add", "新增菜品", {});
      });

      $("#addSetmeal").click(function (e) {
        e.preventDefault();
        Fast.api.open("setmeal/add", "新增套餐", {});
      });

    },
    api: {
      bindevent: function () {
        Form.api.bindevent($("form[role=form]"));
      },
    },
  };

  return Controller;
});
